import { emptyHandle, timestampToTime } from '@vc/utils';
import type { IColumnsOption } from 'vc';
import { INDEX, renderFileJump } from '@/utils/tableColumn';
import { toF9 } from '../../../../utils';
import { typeList } from './constants';

export function useTable () {
  const columns = ref<IColumnsOption[]>([
    { ...INDEX },
    {
      width: 100,
      field: 'code',
      title: '消息类型',
      align: 'center',
      sortable: true,
      slots: {
        default: ({ row }) => {
          const label = typeList.filter((item) => {
            return item.code === row?.type;
          })?.[0]?.name;
          
          return emptyHandle(label);
        }
      }
    },
    {
      field: 'title',
      align: 'left',
      title: '标题',
      slots: {
        default: ({ row }) => renderFileJump(row?.body, row?.body?.title)
      }
    },
    {
      width: 267,
      title: '相关企业',
      align: 'left',
      field: 'label',
      slots: {
        default: ({ row }) => {
          const companyInfo = row.body?.related?.[0];
          return companyInfo?.name ?
            <span
              onClick={companyInfo?.id ? () => toF9(companyInfo?.id) : ()=>{}}
              class={[companyInfo?.id ? 'common-link' : '']}>{companyInfo?.name}</span> : '-';
        }
      }
    },
    {
      width: 111,
      align: 'center',
      field: 'create_time',
      title: '发布日期',
      sortable: true,
      formatter: ({ row }) => {
        return emptyHandle(timestampToTime(row?.body?.date));
      }
    }
  ]);
  
  return {
    columns
  };
}
